package zhoug.demo.zui.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import zhoug.common.utils.StringUtils;
import zhoug.demo.zui.R;
import zhoug.logging.Logger;
import zhoug.zui.popup.ZUIAutoComplete;

public class AutoCompleteActivity extends AppCompatActivity {
    private static final String TAG = ">>>AutoCompleteActivity";
    private EditText mEditText;
    private ZUIAutoComplete<String> autoComplete;
    private List<String> data=new ArrayList<>();

    private String value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_complete);
        findViews();
        initData();
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString().trim();
                if(!text.equals(value)){
                    Logger.d(TAG, "text:"+text);
                    show(text);
                }else{
                    Logger.d(TAG, "未改变:");

                }
            }
        });

    }

    private void findViews() {
        mEditText = findViewById(R.id.editText);
    }


    private void show(String text){
        if(autoComplete==null){
            autoComplete=new ZUIAutoComplete<String>(this) {
                @Override
                public String getText(String itemData) {
                    return itemData;
                }
            };
            autoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    autoComplete.dismiss();
                    value =autoComplete.getData().get(position);
                    mEditText.setText(value);
                }
            });
            autoComplete.setAnchorView(mEditText);
            autoComplete.setDividerColor(Color.parseColor("#aaaaaa"));
            autoComplete.setDividerHeight(2);
            autoComplete.setShowDivider(true);
        }
        autoComplete.setData(getData(text));
        if(autoComplete.isShowing()){
            autoComplete.notifyDataSetChanged();
        }else{
            autoComplete.show();
        }

    }

    private void initData(){
        for(int i=0;i<100;i++){
            data.add("item"+i);
        }
        data.add("zxczx");
        data.add("hgf");
        data.add("中国");
        data.add("中国zxc");
        data.add("中85xc");
        data.add("中8zxczxc5xc");
        data.add("重庆");
        data.add("啊复合肥");
        data.add("123的方块关联的");
    }

    private List<String> getData(String s){
        List<String> temp=new ArrayList<>();
        if(StringUtils.isNullOrEmpty(s)){
            return temp;
        }
        for(String d:data){
            if(d.contains(s)){
                temp.add(d);
            }
        }
        Logger.d(TAG, "getData:"+temp.size());
        return temp;
    }
}